<?php

class Questions extends CI_Controller
{
    var $data = array ();
    var $translate;

    function __construct ()
    {
        parent::__construct ();

        if ( !$this->session->userdata ( 'logged_in' ) )
            redirect(site_url('login'));

        $this->load->model ( 'questions_model' );

        $this->load->helper ( 'inflector' );
        $this->load->config ( 'languages' );
        //$this->translate = & $this->factory->create ( 'translate', 'foo', 'bar' );
        $this->lang->load('common');
    }

    function index ()
    {
        $this->view_all();
    }
    public function view_all ()
    {
        $questions = $this->questions_model->find_all();

        $this->firephp->log($questions);


        $this->data['questions'] = $questions;
        $this->data['title'] = "All Questions";
        $this->data['page_title'] = "Questions";

        $this->template->build ( 'questions/view_all', $this->data );
    }

    public function view_replay_question($question_id)
    {
        $question = $this->questions_model->find_id($question_id);

        $this->firephp->log($question);

        $this->data = array_merge ( $this->data, array (
            'question' => $question
        ) );

        $this->template->build ( 'questions/replay_view', $this->data );
    }

    /*
    public function view_new_question()
    {
        $pages = $this->pages_model->find_all();
        $this->data = array_merge ( $this->data, array (
            'post_action' => 'NEW',
            'pages' => $pages
        ) );
        $this->template->build ( 'questions/new_edit_question', $this->data );
    }
    */

    /*
    public function view_edit_question($question_id)
    {
        $pages = $this->pages_model->find_all();
        $question = $this->Questions_model->find_id($question_id);
        $this->data = array_merge ( $this->data, array (
            'post_action' => 'EDIT',
            'pages' => $pages,
            'question' => $question
        ) );
        $this->template->build ( 'questions/new_edit_question', $this->data );
    }
    */

    public function submit_replay()
    {
        $this->firephp->log($_POST);

        //Post data sent by view
        $post = $_POST;

        $data = array (
            'replay_text' => $post['replay_text'],
            'replay_time' => time()
        );

        $insert_id = null;
        $id = $post['question_id'];

        $this->db->trans_start ();
        $insert_id = $this->questions_model->update($id, $data);
        $this->db->trans_complete ();


        if($insert_id){
            
            $this->load->library('mail_send');
            $question = $this->questions_model->find_id($id);

            $this->mail_send->setTo($question['email']);
            $this->mail_send->setFrom('info@drmedan.me');
            $this->mail_send->setSubject('Odgovor - ' . $question['question_title']);
            $this->mail_send->setMessage($this->load->view('email/mail_template', $question, TRUE));

            $success = $this->mail_send->send();
            
            if($success){
            echo json_encode ( array (
                "action" => true,
                "msg" => lang('common.successfullyoperation'),
                "insert_id" => $insert_id ) );
            }
            else
            {
                echo json_encode ( array (
                    'action' => FALSE,
                    'msg' => '<p>' . lang('common.thisshouldnothappen') . '</p>'
                ) );
            }
        }
        else
        {

            // If comes to thisd line, error ocured
            echo json_encode ( array (
                'action' => FALSE,
                'msg' => '<p>' . lang('common.thisshouldnothappen') . '</p>'
            ) );
        }

    }

    function delete_question ( $question_id )
    {
        $this->questions_model->delete ( $question_id );
    }



}
?>